home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_01_07 / 1n07011b < prev    next >
Text File  |  1990-11-03  |  819b  |  33 lines

  1.  
  2. //
  3. // comma_long function: convert long to ASCII,
  4. // with commas every three digits and sign preservation:
  5. //
  6.  
  7. #include <stdio.h>
  8. #include <math.h>               // needed for labs()
  9.  
  10. void comma_long(long number, char *string);
  11.  
  12. void main()      // test calls to comma_long() function
  13. {
  14.         char string[100];
  15.  
  16.         comma_long(123456L, string);
  17.         printf("result 1: %s\n", string);
  18.  
  19.         comma_long(-2323435L, string);
  20.         printf("result 2: %s\n", string);
  21. }
  22.  
  23. void comma_long(long number, char *string)
  24. {
  25.         if (labs(number) >= 1000) {
  26.                 comma_long(number/1000, string);
  27.                 sprintf(string + strlen(string), ",%03ld",
  28.                         labs(number) % 1000);
  29.         }
  30.         else
  31.                 sprintf(string, "%ld", number);
  32. }
  33.